package com.system.cacheCenterBackend.controller.redisDataController;

import cn.hutool.core.util.ArrayUtil;
import com.system.cacheCenterBackend.controller.hotKeyController.HotKeyController;
import com.system.cacheCenterBackend.service.SetCacheService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Set;

/**
 * Set缓存控制器
 * 提供Set类型的RESTful API接口
 */
@RestController
@RequestMapping("/cache/set")
@RequiredArgsConstructor
public class SetCacheController {

    private final SetCacheService setCacheService;

    /**
     * 设置Set缓存值
     *
     * @param key   Set键
     * @param value Set值
     * @return 统一返回结果
     */
    @PostMapping("/{key}")
    public CacheR<Boolean> set(@PathVariable String key, @RequestBody Set<String> value) {
        try {
            if (setCacheService.setAdd(key, ArrayUtil.toArray(value.iterator(), String.class)) != 0) {
                // 添加键到热点数据控制器中
                HotKeyController.addKey("set", key);
                return CacheR.success(true, "设置Set缓存成功");
            } else {
                return CacheR.fail("设置Set缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置Set缓存异常: " + e.getMessage());
        }
    }

    /**
     * 获取Set缓存值
     *
     * @param key Set键
     * @return 统一返回结果
     */
    @GetMapping("/{key}")
    public CacheR<Set<String>> get(@PathVariable String key) {
        try {
            Set<String> value = setCacheService.get(key);
            if (value != null) {
                return CacheR.success(value, "获取Set缓存成功");
            } else {
                return CacheR.fail(404, "Set缓存不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取Set缓存异常: " + e.getMessage());
        }
    }

    /**
     * 删除Set缓存值
     *
     * @param key Set键
     * @return 统一返回结果
     */
    @DeleteMapping("/{key}")
    public CacheR<Boolean> delete(@PathVariable String key) {
        try {
            boolean result = setCacheService.delete(key);
            if (result) {
                // 从热点数据控制器中移除键
                HotKeyController.removeKey("set", key);
                return CacheR.success(true, "删除Set缓存成功");
            } else {
                return CacheR.fail("删除Set缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("删除Set缓存异常: " + e.getMessage());
        }
    }

    /**
     * 设置Set缓存过期时间
     *
     * @param key     Set键
     * @param timeout 过期时间（秒）
     * @return 统一返回结果
     */
    @PostMapping("/{key}/expire")
    public CacheR<Boolean> expire(@PathVariable String key, @RequestParam long timeout) {
        try {
            boolean result = setCacheService.expire(key, timeout);
            if (result) {
                return CacheR.success(true, "设置过期时间成功");
            } else {
                return CacheR.fail("设置过期时间失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置过期时间异常: " + e.getMessage());
        }
    }
}